package com.jinmdz.fmis.api.api.service;

import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.model.config.ConfigData;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.common.util.DateUtil;
import com.jinmdz.fmis.dao.dao.ChargeSettleDao;
import com.jinmdz.fmis.dao.dao.ChargeVerifyDao;
import com.jinmdz.fmis.dao.dao.FuneralBusinessDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * FuneralBusinessService类
 *
 * @author LiCongLu
 * @date 2020-02-06 9:00
 */
@Service("funeralBusinessService")
public class FuneralBusinessService extends BaseService {

    @Resource
    private ConfigData configData;

    @Resource
    private FuneralBusinessDao funeralBusinessDao;

    @Resource
    private ChargeSettleDao chargeSettleDao;

    @Resource
    private ChargeVerifyDao chargeVerifyDao;

    /**
     * 获取新的业务编码
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-02-06 9:48
     */
    public String getBusinessCodeAsNew() {
        Date nowDate = nowDate();

        // 前缀编码
        String prefixCode = configData.getFuneralBusinessCodePrefix() + DateUtil.formatPattern(nowDate, configData.getFuneralBusinessCodeDatePattern());
        // 自增码长度
        int autoLength = configData.getFuneralBusinessCodeAutoLength();

        // 自增起始码
        int autoNumber = 0;

        // 最后的业务编码
        String lastCode = funeralBusinessDao.getBusinessCodeAsLast();

        // 存在最后的业务编码，且是当天的业务编码
        if (DataUtil.valid(lastCode) && lastCode.startsWith(prefixCode)) {
            // 得到最后的自增编码
            lastCode = lastCode.replace(prefixCode, "");
            int lastNumber = Integer.parseInt(lastCode);
            autoNumber = lastNumber;
        }

        // 拼接最新业务编码
        String newCode = prefixCode + String.format("%0" + String.valueOf(autoLength) + "d", ++autoNumber);
        return newCode;
    }

    /**
     * 生成新的费用结算批次
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-02-19 18:07
     */
    public String getSettleBatchNoAsNew() {
        Date nowDate = nowDate();

        // 前缀编码
        String prefixCode = configData.getSettleBatchNoPrefix() + DateUtil.formatPattern(nowDate, configData.getSettleBatchNoDatePattern());
        // 自增码长度
        int autoLength = configData.getSettleBatchNoAutoLength();

        // 自增起始码
        int autoNumber = 0;

        // 最后的费用结算批次编号
        String lastCode = chargeSettleDao.getSettleBatchNoAsLast();

        // 存在最后的费用结算批次编号，且是当天的费用结算批次编号
        if (DataUtil.valid(lastCode) && lastCode.startsWith(prefixCode)) {
            // 得到最后的自增编码
            lastCode = lastCode.replace(prefixCode, "");
            int lastNumber = Integer.parseInt(lastCode);
            autoNumber = lastNumber;
        }

        // 拼接最费用结算批次编号
        String newCode = prefixCode + String.format("%0" + String.valueOf(autoLength) + "d", ++autoNumber);
        return newCode;
    }

    /**
     * 生成新的日结审核批次
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-02-19 18:07
     */
    public String getVerifyBatchNoAsNew() {
        Date nowDate = nowDate();

        // 前缀编码
        String prefixCode = configData.getVerifyBatchNoPrefix() + DateUtil.formatPattern(nowDate, configData.getVerifyBatchNoDatePattern());
        // 自增码长度
        int autoLength = configData.getVerifyBatchNoAutoLength();

        // 自增起始码
        int autoNumber = 0;

        // 最后的日结审核批次编号
        String lastCode = chargeVerifyDao.getVerifyBatchNoAsLast();

        // 存在最后的日结审核批次编号，且是当天的日结审核批次编号
        if (DataUtil.valid(lastCode) && lastCode.startsWith(prefixCode)) {
            // 得到最后的自增编码
            lastCode = lastCode.replace(prefixCode, "");
            int lastNumber = Integer.parseInt(lastCode);
            autoNumber = lastNumber;
        }

        // 拼接最日结审核批次编号
        String newCode = prefixCode + String.format("%0" + String.valueOf(autoLength) + "d", ++autoNumber);
        return newCode;
    }
}
